/* 
 * Directives
 *
 */
angular.module('app')

.directive("edit", function($document){
    return{
        restrict: "AE",
        require: 'ngModel',
		scope : {
			dType : '@',
			showStockEdit : '=',
			showCatEdit : '=',
			master : "="
        },
        link: function(scope,element,attrs,ngModel){
            element.bind("click",function(){
				// disabled edit/update/cancal functionalities
				// return;
				// must get id before following angular.copy			
				if (scope.dType == "stock" || scope.dType == "selfSelect") {
					var id = "stock_code_" +ngModel.$modelValue.code;
					scope.$apply(function(){
						scope.showStockEdit = false;
					})
				} else {
					var id = "category_name_" +ngModel.$modelValue.name;
					scope.$apply(function(){
						scope.showCatEdit = false;
					})
				}
					
				var obj = $("#"+id);
                obj.removeClass("inactive");
                obj.addClass("active");
                obj.removeAttr("readOnly");
				obj.focus();
			
				scope.$apply(function(){
                    angular.copy(ngModel.$modelValue,scope.master);
                })
            })
        }
    }
})

.directive("update",function($document){
    return{
        restrict: 'AE',
        require: 'ngModel',
		scope : {
			dType : '@',
			showStockEdit : '=',
			showCatEdit : '='
        },
        link: function(scope,element,attrs,ngModel){
            element.bind("click",function(){
				// disabled edit/update/cancal/delete functionalities
				// return;
				if (scope.dType == "stock" || scope.dType == "selfSelect") {
					var id = "stock_code_" +ngModel.$modelValue.code;
					scope.$apply(function(){
						scope.showStockEdit = true;
					})
				} else {
					var id = "category_name_" +ngModel.$modelValue.name;
					scope.$apply(function(){
						scope.showCatEdit = true;
					})
				}
				
                var obj = $("#"+id);
                obj.removeClass("active");
                obj.addClass("inactive");
                obj.attr("readOnly",true);
            })
        }
    }
})
    
.directive("cancel",function($document){
    return{
        restrict: 'AE',
        require: 'ngModel',
		scope : {
			dType : '@',
			showStockEdit : '=',
			showCatEdit : '=',
			master : "="
        },
        link: function(scope,element,attrs,ngModel){
            element.bind("click",function(){
				// disabled edit/update/cancal/delete functionalities
				// return;
				if (scope.dType == "stock" || scope.dType == "selfSelect") {
					var id = "stock_code_" +ngModel.$modelValue.code;
					scope.$apply(function(){
						scope.showStockEdit = true;
					})
				} else {
					var id = "category_name_" +ngModel.$modelValue.name;
					scope.$apply(function(){
						scope.showCatEdit = true;
					})
				}
				
                var obj = $("#"+id);
                obj.removeClass("active");
                obj.addClass("inactive");
                obj.prop("readOnly",true);
				
				scope.$apply(function(){
                    angular.copy(scope.master,ngModel.$modelValue);
                })
            })
        }
    }
})

.directive("delete", function($document){
    return{
        restrict:'AE',
        require: 'ngModel',
		scope : {
			deleteFunc : '&',
        },
        link:function(scope, element, attrs,ngModel){
            element.bind("click",function(){
				// disabled edit/update/cancal/delete functionalities
				// return;
                if (!confirm("Do you want to delete it?")) {
                    window.event.returnValue = false;
                } else {
					scope.deleteFunc(ngModel.$modelValue.id);
                }
            })
        }
    }
})

.directive("editableStockcode", ['StockService', function(StockService){
    return{
        restrict:'AE',
        require: 'ngModel',
        link:function(scope, element, attrs,ngModel){
            element.bind("click",function(){
                $('#warningMsgForStock').html("");
		
				$(this).removeClass("inactive");
				$(this).addClass("active");
				$(this).removeAttr("readOnly");
				$(this).focus();
				
				scope.oldStockcode = $(this).val();
            })
			element.bind("blur",function(){		
				$(this).removeClass("active");
				$(this).addClass("inactive");
				$(this).attr("readOnly",true);
				scope.showStockEdit=true;
				
				var newStockcode = ngModel.$modelValue;
				if (!newStockcode) {
					// value not inputed
					$('#warningMsgForStock').html("<font color='red'>Please enter specific stock code!</font>");
					$(this).removeClass("inactive");
					$(this).addClass("active");
					$(this).removeAttr("readOnly");
					$(this).focus();
					scope.oldStockcode = $(this).val();
					return;
				} 
				if (newStockcode == scope.oldStockcode) {
					// value not changed
					return;
				}
				
				// in category list page
				if (scope.currentPage=="CategoryMgr") {
					StockService.saveStockToCategory(newStockcode, scope.selectedCategory.id).success( 
						function(data) {
							for(var i=0; i<scope.stocksByCategory.length; i++){
								if(scope.stocksByCategory[i].newStockcode==data.newStockcode){
									scope.stocksByCategory[i] = data;
								}
							}
						}
					).error(
						function(data, status, headers, config) {
							$('#warningMsgForStock').html("<font color='red'>An error happened, please contact system administrator!</font>");
						}
					);
				// in stock list page
				} else if (scope.currentPage=="StockMgr") {
					if (scope.currentTab="selfselect") {
						StockService.saveSelfSelectByCode(newStockcode).success( 
							function(data) {
								for(var i=0; i<scope.selfSelects.length; i++){
									if(scope.selfSelects[i].newStockcode==data.newStockcode){
										scope.selfSelects[i] = data;
									}
								}
							}
						).error(
							function(data, status, headers, config) {
								$('#warningMsgForStock').html("<font color='red'>An error happened, please contact system administrator!</font>");
							}
						);
					} else {
						StockService.saveStockbyCode(newStockcode).success( 
							function(data) {
								for(var i=0; i<scope.stocks.length; i++){
									if(scope.stocks[i].newStockcode==data.newStockcode){
									scope.stocks[i] = data;
									}
								}
							}
						).error(
							function(data, status, headers, config) {
								$('#warningMsgForStock').html("<font color='red'>An error happened, please contact system administrator!</font>");
							}
						);
					}
					
				}
			})
			
        }
    }
}])

.directive("editableCategoryname", ['StockCategoryService', function(StockCategoryService){
    return{
        restrict:'AE',
        require: 'ngModel',
        link:function(scope, element, attrs,ngModel){
            element.bind("click",function(){
                $('#warningMsgForCat').html("");
		
				$(this).removeClass("inactive");
				$(this).addClass("active");
				$(this).removeAttr("readOnly");
				$(this).focus();
            })
			element.bind("blur",function(){		
				$(this).removeClass("active");
				$(this).addClass("inactive");
				$(this).attr("readOnly",true);
				scope.showCatEdit=true;
				
				var catName = ngModel.$modelValue;
				if (scope.currentPage=="StockMgr") {
					StockCategoryService.saveStockCategoryWithCode(scope.selectedStock.code, catName).success( 
						function(data) {
							for(var i=0; i<scope.categories.length; i++){
								if(scope.categories[i].name==data.name){
									scope.categories[i] = data;
								}
							}
						}
					).error(
						function(data, status, headers, config) {
							$('#warningMsgForCat').html("<font color='red'>An error happened, please contact system administrator!</font>");
						}
					);
				} else if (scope.currentPage=="CategoryMgr"){
					StockCategoryService.saveStockCategory(catName).success( 
						function(data) {
							for(var i=0; i<scope.categories.length; i++){
								if(scope.categories[i].name==data.name){
									scope.categories[i] = data;
								}
							}
						}
					).error(
						function(data, status, headers, config) {
							$('#warningMsgForCat').html("<font color='red'>An error happened, please contact system administrator!</font>");
						}
					);
				}
				
			})
			
        }
    }
}])
